package com.linciping.gradle.test;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.ContextWrapper;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.Spinner;

import androidx.annotation.Nullable;

/**
 * 注入工具类
 */
public class InjectorUtils {

    /**
     * 判断是否是fragment
     *
     * @param obj
     * @return
     */
    public static boolean isFragment(Object obj) {
        try {
            if (obj == null) {
                return false;
            }
            Class<?> supportFragmentClass = null;
            Class<?> androidXFragmentClass = null;
            Class<?> fragment = null;
            try {
                fragment = Class.forName("android.app.Fragment");
            } catch (Exception e) {
                errorLog(e.getMessage());
            }
            try {
                supportFragmentClass = Class.forName("android.support.v4.app.Fragment");
            } catch (Exception e) {
                errorLog(e.getMessage());
            }

            try {
                androidXFragmentClass = Class.forName("androidx.fragment.app.Fragment");
            } catch (Exception e) {
                errorLog(e.getMessage());
            }

            if (supportFragmentClass == null && androidXFragmentClass == null && fragment == null) {
                return false;
            }

            if ((supportFragmentClass != null && supportFragmentClass.isInstance(obj)) ||
                    (androidXFragmentClass != null && androidXFragmentClass.isInstance(obj)) ||
                    (fragment != null && fragment.isInstance(obj))) {
                return true;
            }
        } catch (Exception e) {
            errorLog(e.getMessage());
        }
        return false;
    }

    /**
     * 找到activity
     *
     * @param context
     * @return
     */
    public static Activity getActivityFromContext(Context context) {
        Activity activity = null;
        try {
            if (context != null) {
                if (context instanceof Activity) {
                    activity = (Activity) context;
                } else if (context instanceof ContextWrapper) {
                    while (!(context instanceof Activity) && context instanceof ContextWrapper) {
                        context = ((ContextWrapper) context).getBaseContext();
                    }
                    if (context instanceof Activity) {
                        activity = (Activity) context;
                    }
                }
            }
        } catch (Exception e) {
            errorLog(e.getMessage());
        }
        return activity;
    }

    /**
     * 找到activity
     *
     * @param fragmentObj
     * @return
     */
    public static Activity getActivityFromFragment(Object fragmentObj) {
        Activity activity = null;
        try {
            if (fragmentObj instanceof Fragment) {
                activity = ((Fragment) fragmentObj).getActivity();
            } else if (fragmentObj instanceof androidx.fragment.app.Fragment) {
                activity = ((androidx.fragment.app.Fragment) fragmentObj).getActivity();
            }
        } catch (Exception e) {
            errorLog(e.getMessage());
        }
        return activity;
    }


    public static void recursiveChildView(ViewGroup viewGroup, String fragmentName) {
        if (viewGroup == null || TextUtils.isEmpty(fragmentName)) return;
        final int childCount = viewGroup.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = viewGroup.getChildAt(i);
            child.setTag(R.id.inject_fragment_name, fragmentName);
            if (child instanceof ViewGroup && !(child instanceof ListView ||
                    child instanceof GridView ||
                    child instanceof Spinner ||
                    child instanceof RadioGroup)) {
                recursiveChildView((ViewGroup) child, fragmentName);
            }
        }
    }

    @Nullable
    public static String recursiveView2FragmentName(View view) {
        try {
            ViewParent parentView = view.getParent();
            String fragmentName = null;
            while (TextUtils.isEmpty(fragmentName) && parentView instanceof View) {
                fragmentName = (String) ((View) parentView).getTag(R.id.inject_fragment_name);
                parentView = parentView.getParent();
            }
            return fragmentName;
        } catch (Exception ex) {
            errorLog(ex.getMessage());
        }
        return null;
    }

    public static void errorLog(String msg) {
        Log.e("InjectorUtils", msg);
    }
}
